package com.ayoka.ui.assignment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ayoka.R;
import com.ayoka.adapter.CourseAssignmentInputOptionAdapter;
import com.ayoka.base.BaseRecyclerViewFragment;
import com.ayoka.bean.CourseAssignmentBean.QuestionBean;
import com.ayoka.databinding.FragmentCourseAssignmentRecyclerViewBinding;
import com.ayoka.mvp.CourseAssignmentMVP.CourseAssignmentPresenter;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;

public class CourseAssignmentInputFragment extends BaseRecyclerViewFragment<QuestionBean, FragmentCourseAssignmentRecyclerViewBinding, CourseAssignmentPresenter> {

    public static CourseAssignmentInputFragment newInstance(QuestionBean assignmentQuestionBean, String currentChoiceIndex) {
        CourseAssignmentInputFragment assignmentFragment = new CourseAssignmentInputFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("AssignmentQuestionBean", assignmentQuestionBean);
        bundle.putString("CurrentChoiceIndex", currentChoiceIndex);
        assignmentFragment.setArguments(bundle);
        return assignmentFragment;
    }

    protected String currentChoiceIndex;
    protected QuestionBean assignmentQuestionBean;

    @Override
    protected void configBundleArguments() {
        assignmentQuestionBean = (QuestionBean) getArguments().getSerializable("AssignmentQuestionBean");
        currentChoiceIndex = getArguments().getString("CurrentChoiceIndex");
    }


    @Override
    public int getContentLayout() {
        return R.layout.fragment_course_assignment_recycler_view;
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        View headerView = View.inflate(getActivity(), R.layout.header_course_assignment, null);
        headerView.findViewById(R.id.ll_choice_description).setVisibility(View.GONE);
        headerView.findViewById(R.id.ll_grade_description).setVisibility(View.GONE);
        headerView.findViewById(R.id.ll_input_description).setVisibility(View.VISIBLE);
        TextView tvIndex = (TextView) headerView.findViewById(R.id.tv_question_index);
        tvIndex.setText(currentChoiceIndex);
        TextView tvTitle = (TextView) headerView.findViewById(R.id.tv_input_title);
        tvTitle.setText(assignmentQuestionBean.Question);
        adapter.addHeaderView(headerView);

        View footerView = View.inflate(getActivity(), R.layout.footer_course_assignment, null);
        Button btnNextQuestion = (Button) footerView.findViewById(R.id.btn_complete);
        btnNextQuestion.setOnClickListener(this);
        adapter.addFooterView(footerView);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return null;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @Override
    protected BaseQuickAdapter<QuestionBean, BaseViewHolder> getAdapter() {
        CourseAssignmentInputOptionAdapter adapter = new CourseAssignmentInputOptionAdapter();
        return adapter;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected boolean getRefreshEnable() {
        return false;
    }

    @Override
    protected boolean getLoadEnable() {
        return false;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
    }

    @Override
    public void initData() {
        adapter.addData(assignmentQuestionBean);
    }

    @Override
    protected CourseAssignmentPresenter getPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.btn_complete) {
            String inputContent = ((CourseAssignmentInputOptionAdapter)adapter).getInputContent();
            if (TextUtils.isEmpty(inputContent)){
                showToast("请输入内容");
                return;
            }
            assignmentQuestionBean.AdmixtureAnswer.WordAnswer = inputContent;
            ((CourseAssignmentAdmixtureFragment)getParentFragment()).showNextQuestion();
        }
    }

}
